
use "$basefiles\\$basefile_studentRoster", clear
keep if year_dose > 0
drop stu_id year_dose
drop if year > 2012
duplicates drop id year subject, force

sort id year subject
egen taught_math = max(subject=="Math"), by(id year)
egen taught_reading = max(subject=="ELA"), by(id year)
duplicates drop id year, force
drop subject

sort id year
by id: gen taught_math_L1 = taught_math[_n-1]
by id: gen taught_reading_L1 = taught_reading[_n-1]

by id: gen switch_out_math = taught_math_L1 == 1 & taught_math == 0 if taught_math_L1 != .
by id: gen switch_in_math = taught_math_L1 == 0 & taught_math == 1 if taught_math_L1 != .
by id: gen switch_out_read = taught_reading_L1 == 1 & taught_reading == 0 if taught_reading_L1 != .
by id: gen switch_in_read = taught_reading_L1 == 0 & taught_reading == 1 if taught_reading_L1 != .

save "$temp_dir\teacher_subject_switching", replace
